package com.ticket.sass.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.dto.output.order.TicketOutput;
import com.ticket.sass.admin.mapper.OrderGroupPurchaseDetailMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.OrderGroupPurchaseDetailEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-08-14
 */
@Service
public class OrderGroupPurchaseDetailServiceImpl extends ServiceImpl<OrderGroupPurchaseDetailMapper, OrderGroupPurchaseDetailEntity> implements BaseService<OrderGroupPurchaseDetailEntity> {
    // 根据订单票号列表获取票详情信息
    public Map<Long, List<TicketOutput>> getTicketDetailsByOrderTicketIds(List<Long> orderGroupPurchaseId) {
        if (orderGroupPurchaseId.size() == 0){
            return new HashMap<>();
        }
        
        Map<Long, List<TicketOutput>> ticketDetailsMap = new HashMap<>();
        
        // 使用 MyBatis-Plus 的 selectBatchIds 方法查询指定订单票号列表的票详情
        List<OrderGroupPurchaseDetailEntity> orderTicketDetails = this.list(new QueryWrapper<OrderGroupPurchaseDetailEntity>().in("order_group_purchase_id", orderGroupPurchaseId));
        
        // 将查询结果转换为所需的格式
        for (OrderGroupPurchaseDetailEntity orderTicketDetail : orderTicketDetails) {
            // 创建票详情对象
            TicketOutput ticketOutput = new TicketOutput();
            ticketOutput.setSeat(orderTicketDetail.getSeat());
            ticketOutput.setGbCode(orderTicketDetail.getGbCode());
            ticketOutput.setPrice(orderTicketDetail.getParPrice());
            ticketOutput.setSeatStatus(orderTicketDetail.seatStatus().getValue());
            
            BigDecimal serviceFee = orderTicketDetail.getServiceFee();
            BigDecimal handleFee = orderTicketDetail.getHandleFee();
            BigDecimal channelServiceFee = orderTicketDetail.getChannelServiceFee();
            
            if (serviceFee == null) {
                serviceFee = BigDecimal.ZERO;
            }
            if (handleFee == null) {
                handleFee = BigDecimal.ZERO;
            }
            if (channelServiceFee == null) {
                channelServiceFee = BigDecimal.ZERO;
            }
            
            BigDecimal totalServiceFee = serviceFee.add(handleFee).add(channelServiceFee);
            ticketOutput.setServiceFee(totalServiceFee);
            
            // 将票详情对象添加到对应的订单票号键下的列表中
            List<TicketOutput> ticketList = ticketDetailsMap.getOrDefault(orderTicketDetail.getOrderGroupPurchaseId(), new ArrayList<>());
            ticketList.add(ticketOutput);
            ticketDetailsMap.put(orderTicketDetail.getOrderGroupPurchaseId(), ticketList);
        }
        
        
        return ticketDetailsMap;
    }
}
